'use strict';

/* eslint-disable */
$(function () {
  var now = journals
  var page = 1
  var pageCount = 1
  getJournals(page)

  function getJournals(p){
    $("section").remove();
    $("li").remove(".pageicon");
    $.get('http://39.106.171.39:8011/mbl/Article/getArticleList?pagesize=10&page='+ p, function(data, status){
      console.log('status:', status);
      console.log('data:', data);
      let datas = data.rows;
      pageCount = data.pageCount
      for (let i=0; i<datas.length; i++){
        let main = $(".main");
        let section = $('<section class="new border-bottom-line"></section>')
        let newitem = $('<div class="desc"></div>');
        let title = $('<h3></h3>').text(datas[i].title)
        let author = $('<p></p>').text(datas[i].author)
        let journalname = $('<p></p>').text(datas[i].journalname)
        let doi = $('<span class="mark"></span>').text(": DOI:"+datas[i].doi)
        journalname.append(doi)
        let publishdate = $('<p></p>').text("Publication Date: "+datas[i].publishdate)
        let img = $('<img>').attr("src", datas[i].picture).attr("alt","");
        newitem.append(img).append(title).append(author).append(author).append(journalname).append(publishdate);
        section.append(newitem);
        main.prepend(section);
      }
      // 分页列表
      for (let i = 0; i < data.pageCount; i++ ) {
        let num = i + 1;
        let item = $("<li class='pageicon'></li>");
        let aElem = $('<span></span>').text(num);
        item.append(aElem);
        item.click(function (e) {
          // e.preventDefault()
          // window.location.href = window.location.pathname + "?page=" + num;
          getJournals(num)
        });
        item.insertBefore($("#next"));
      }
    });
  }
  function getBooks(p){
    $("section").remove();
    $("li").remove(".pageicon");
    $.get('http://39.106.171.39:8011/mbl/Book/getBookList?pagesize=10&page='+ p, function(data, status){
      console.log('status:', status);
      console.log('data:', data);
      let datas = data.rows;
      pageCount = data.pageCount
      for (let i=0; i<datas.length; i++){
        let main = $(".main");
        let section = $('<section class="publication border-bottom-line"></section>')
        let newitem = $('<div class="item publication-desc"></div>');
        let bookname = $('<h3></h3>').text(datas[i].bookname)
        let publisher = $('<p></p>').text(datas[i].publisher)
        let author = $('<p></p>').text(datas[i].author)
        let introduction = $('<p></p>').text(datas[i].introduction)
        let img = $('<img>').attr("src", datas[i].picture).attr("alt","");
        newitem.append(bookname).append(publisher).append(author).append(introduction);
        section.append(img).append(newitem);
        main.prepend(section);
      }
      // 分页列表
      for (let i = 0; i < data.pageCount; i++ ) {
        let num = i + 1;
        let item = $("<li class='pageicon'></li>");
        let aElem = $('<span></span>').text(num);
        item.append(aElem);
        item.click(function (e) {
          // e.preventDefault()
          // window.location.href = window.location.pathname + "?page=" + num;
          getBooks(num)
        });
        item.insertBefore($("#next"));
      }
    });
  }
  function getPatents(p){
    $("section").remove();
    $("li").remove(".pageicon");
    $.get('http://39.106.171.39:8011/mbl/Patent/getPatentList?pagesize=10&page='+ p, function(data, status){
      console.log('status:', status);
      console.log('data:', data);
      let datas = data.rows;
      pageCount = data.pageCount
      for (let i=0; i<datas.length; i++){
        let main = $(".main");
        let section = $('<section class="new border-bottom-line"></section>')
        let newitem = $('<div class="desc"></div>');
        let title = $('<h3></h3>').text(datas[i].title)
        let number = $('<p></p>').text("Patent Number: " + datas[i].number)
        let assignee = $('<p></p>').text("Patent Assignee: "+datas[i].assignee)
        let inventor = $('<p></p>').text("Patent inventor: "+datas[i].inventor)
        newitem.append(title).append(number).append(assignee).append(inventor);
        section.append(newitem);
        main.prepend(section);
      }
      // 分页列表
      for (let i = 0; i < data.pageCount; i++ ) {
        let num = i + 1;
        let item = $("<li class='pageicon'></li>");
        let aElem = $('<span></span>').text(num);
        item.append(aElem);
        item.click(function (e) {
          // e.preventDefault()
          // window.location.href = window.location.pathname + "?page=" + num;
          getPatents(num)
        });
        item.insertBefore($("#next"));
      }
    });
  }

  $("#journals").click(function(){
    now = journals
    page = 1
    getJournals(page)
  });
  $("#books").click(function(){
    now = books
    page = 1
    getBooks(page)
  });
  $("#patents").click(function(){
    now = patents
    page = 1
    getPatents(page)
  });
  $("#search").click(function(){

  });




  // 分页
  var prev = $("#prev");
  prev.click(function () {
    // 获取路径参数
    if (page && Number(page) > 1) {
      page = Number(page) - 1;
      // window.location.href = window.location.pathname + "?page=" + page;
      if(now=='journals'){
        getJournals(page)
      }else if(now=='books'){
        getBooks(page)
      }else if(now=='patents'){
        getPatents(page)
      }
    }
  });
  var next = $("#next");
  next.click(function () {
    // 获取路径参数
    if (page<pageCount) {
      page = Number(page) + 1;
      // window.location.href = window.location.pathname + "?page="+page;
      if(now=='journals'){
        getJournals(page)
      }else if(now=='books'){
        getBooks(page)
      }else if(now=='patents'){
        getPatents(page)
      }
    }
  });



});
